package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.app.util.QiNiuUploadUtil;
import cn.wolfcode.trip.app.util.UploadUtil;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("images")
public class ImageController {


    @Autowired
    private ServletContext sct;
    /**
     * 图片的上传
     * @return
     */
    @PostMapping
    public Object upload(MultipartFile file){
        Map<String,Object> map = new HashMap<String, Object>();
        try {
            // 将用户的头像上传至图片服务器
            String url = QiNiuUploadUtil.upload(file, UploadUtil.BASE_PATH + "/upload");
            // 返回图片的url,用户回显
            map.put("url",url);
            map.put("status",1);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("status",0);

        }
        return map;
    }
}
